<template>
  <el-dialog v-model="dialogFormVisible" :title="title" @close="closeDialog">
    <el-form :model="addClassinfoForm" ref="addClassinfoRef" :rules="addClassinforules">
      <el-form-item label="所在年级" :label-width="formLabelWidth" prop="gradeId">
        <el-select v-model="addClassinfoForm.gradeId" autocomplete="off" @change="filterGradeId" placeholder="请选择年级">
          <el-option v-for="gradeId in gradeIds" :key="gradeId" :value="gradeId"></el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="班级名称" :label-width="formLabelWidth" prop="name">
        <el-input v-model="addClassinfoForm.name" autocomplete="off" readonly ref="name" placeholder="请填写名称"></el-input>
      </el-form-item>

      <el-form-item label="班主任" :label-width="formLabelWidth" prop="teacherName">
        <el-select v-model="addClassinfoForm.teacherNumber" filterable @change="filterData" placeholder="请选择人员姓名">
          <el-option v-for="item in list" :key="item.teacherNumber" :label="item.name" :value="item.teacherNumber" />
        </el-select>
      </el-form-item>
      <el-form-item label="所在级部" :label-width="formLabelWidth" prop="subordinateDepartment">
        <el-select v-model="addClassinfoForm.subordinateDepartment" filterable @change="filterDepartmentData"
          placeholder="请选择级部">
          <el-option v-for="item in departmentList" :key="item.id" :label="item.name" :value="item.id" />
        </el-select>
      </el-form-item>
    </el-form>
    <template #footer>
      <span class="dialog-footer">
        <el-button @click="cancel()">取消</el-button>
        <el-button type="primary" @click="save()">保存</el-button>
      </span>
    </template>
  </el-dialog>
</template>

<script>
import { ref } from "vue";
import { httpService } from "@/utils/httpService";
import { ElMessage, ElForm } from "element-plus";
import { vxRule } from "@/utils/validator";

export default {
  name: "AddClassinfo",
  components: { ElForm, ElMessage },
  data() {
    return {
      dialogFormVisible: ref(false),
      formLabelWidth: "120px",
      title: "",
      personnelType: "",
      gradeIds: [],
      list: [],
      departmentList: [],
      addClassinfoForm: {
        modeSubject: "",
        classId: "",
        gradeId: "",
        name: "",
        alias: "",
        level: "",
        type: "",
        status: "",
        campus: "",
        classType: "",
        subordinateDepartment: "",
        subordinateDepartmentName: "",
        subjectType: "",
        isDelete: "",
        classMotto: "",
        schoolYear: "",
        thirdClassCode: "",
        teacherName: "",
        teacherNumber: "",
      },
      // 校验规则
      addClassinforules: {
        name: vxRule(true, null, "blur"),
        gradeId: vxRule(true, null, "blur"),
        subordinateDepartmentName: vxRule(true, null, "blur"),
        classMotto: vxRule(true, null, "blur"),
        schoolYear: vxRule(true, null, "blur"),
        thirdClassCode: vxRule(true, null, "blur"),
        teacherName: vxRule(true, null, "blur"),
        teacherNumber: vxRule(true, null, "blur"),
      },
    };
  },
  methods: {
    filterGradeId(val) {
      //获取班级名称
      httpService("/javaApi/baseClassInfo/getClazzName/" + val, {}, "get").then(
        (data) => {
          this.addClassinfoForm.alias = data.alias;
          this.addClassinfoForm.name = data.alias;

        }
      );
    },
    filterData(val) {
      let obj = this.list.find((item) => {
        return item.teacherNumber === val;
      });
      this.addClassinfoForm.teacherName = obj.name;
    },
    filterDepartmentData(val) {
      let obj = this.departmentList.find((item) => {
        return item.id === val;
      });
      this.addClassinfoForm.subordinateDepartmentName = obj.name;
    },
    getGradeIds() {
      let date = new Date();
      let nowYear = parseInt(date.getFullYear());
      let nowMonth = parseInt(date.getMonth() + 1);
      let gradeIds = [];
      if (nowMonth > 6) {
        gradeIds.push(nowYear - 2);
        gradeIds.push(nowYear - 1);
        gradeIds.push(nowYear);
      } else {
        gradeIds.push(nowYear - 3);
        gradeIds.push(nowYear - 2);
        gradeIds.push(nowYear - 1);
      }
      this.gradeIds = gradeIds;
    },
    getBaseTeacherListApp() {
      httpService("/javaApi/baseTeacher/list/1/10000", {}, "get").then(
        (data) => {
          this.list = data.list;

        }
      );
      this.getBaseDepartmentList()

    },
    closeDialog() {
      this.clear();
    },
    cancel() {
      this.dialogFormVisible = false;
      this.clear();
    },
    getClassinfoById(id) {
      httpService("/javaApi/baseClassInfo/queryById", { id: id }, "get").then(
        (data) => {
          this.addClassinfoForm = data;
        }
      );
    },
    clear() {
      this.dialogFormVisible = false;
      //清空form
      this.addClassinfoForm.modeSubject = "";
      this.addClassinfoForm.id = "";
      this.addClassinfoForm.classId = "";
      this.addClassinfoForm.gradeId = "";
      this.addClassinfoForm.name = "";
      this.addClassinfoForm.alias = "";
      this.addClassinfoForm.level = "";
      this.addClassinfoForm.type = "";
      this.addClassinfoForm.status = "";
      this.addClassinfoForm.campus = "";
      this.addClassinfoForm.classType = "";
      this.addClassinfoForm.subordinateDepartment = "";
      this.addClassinfoForm.subordinateDepartmentName = "";
      this.addClassinfoForm.subjectType = "";
      this.addClassinfoForm.isDelete = "";
      this.addClassinfoForm.classMotto = "";
      this.addClassinfoForm.schoolYear = "";
      this.addClassinfoForm.thirdClassCode = "";
      this.addClassinfoForm.teacherName = "";
      this.addClassinfoForm.teacherNumber = "";
    },
    getBaseDepartmentList() {
      httpService(
        "/javaApi/baseDepartment/list/1/100",
        {
          type: 2,
        },
        "get"
      ).then((data) => {
        this.departmentList = data.list;
      });
    },
    save() {
      this.$refs.addClassinfoRef.validate((valid) => {
        if (valid) {
          let params = new FormData(); //创建一个form对象,必须是form对象否则后端接受不到数据
          params.append("name", this.addClassinfoForm.name);
          params.append("alias", this.addClassinfoForm.alias);
          params.append("teacherName", this.addClassinfoForm.teacherName);
          params.append("teacherNumber", this.addClassinfoForm.teacherNumber);
          params.append("subordinateDepartment", this.addClassinfoForm.subordinateDepartment);
          params.append("subordinateDepartmentName", this.addClassinfoForm.subordinateDepartmentName);
          if (this.addClassinfoForm.id) {
            params.append("id", this.addClassinfoForm.id);
            httpService("/javaApi/baseClassInfo/update", params, "post").then(
              (data) => {
                if (data.code == 200) {
                  ElMessage.success("修改成功");
                  this.updateHandle()
                  this.clear();
                  //刷新父组件的数据
                  this.$parent.getClassinfoList();
                } else {
                  ElMessage.error("修改失败");
                }
              }
            );
          } else {
            httpService("/javaApi/baseClassInfo/add", params, "post").then(
              (data) => {
                if (data.code == 200) {
                  ElMessage.success("添加成功");
                  this.updateHandle()
                  this.clear();
                  //刷新父组件的数据
                  this.$parent.getClassinfoList();
                } else {
                  ElMessage.error("添加失败");
                }
              }
            );
          }
        } else {
          return false;
        }
      });
    },

    updateHandle() {
      let params = new FormData()
      httpService("/javaApi/baseClassInfo/updateMasterRole", params, "post").then(
        (data) => {
          console.log(data);
        }
      );
    },
  },
};
</script>
<style>
.el-select {
  width: 100%;
}
</style>